<?php

/**
* Notification Library
*
* Contains a lot of functions that helps to control and display notification in
* the application (global).
*
* @package 		app
* @subpackage 	libraries
* @author 		Darwing Hernández <darwing.her69@gmail.com>
*
* The original idea was taken from the project Snap.
*
* @link 		https://github.com/AeroCross/Snap
* @author 		Mario Cuba <mario@mariocuba.net>
*/

Class Notification
{
	/**
	* Shortcut to $this->choose();
	* 
	* @param 	string - the notification to display
	* @param 	string - an optional message can be displayed
	* @access 	public 
	*/
	public static function get($notification, $opt = null)
	{
		return self::choose($notification, $opt);
	}

	/**
	* Defines all app notifications
	* 
	* @param 	string - notification case
	* @param 	string - an optional message to be displayed
	* @access   private
	*/
	private static function choose($notification, $opt = null)
	{
		switch ($notification) {
			// creation
			case 'register_add_success': 
				return  array(
					'message' 	=> 	$opt.' ha sido creado con éxito.', 
					'type'		=> 	'success'
				);
			break;
			case 'register_add_warning': 
				return array(
					'message'	=> 	$opt.' no pudo ser creado. Intentelo de nuevo.',
					'type'		=>	'warning'
				);
			break;
			// edition
			case 'register_edit_success': 
				return array(
					'message'	=> 	'La actualización se realizó con éxito.',
					'type'		=>	'success'
				);
			break;
			case 'register_edit_warning': 
				return array(
					'message'	=> 	'No pudo realizarse la actualización. Intente de nuevo.',
					'type'		=>	'warning'
				);
			break;
			// deleting
			case 'register_delete_success': 
				return array(
					'message'	=> 	'Eliminación exitosa de los registros.',
					'type'		=>	'success'
				);
			break;
			case 'register_delete_warning': 
				return array(
					'message'	=> 	'No pude realizarse la eliminación exitosamente. Intente de nuevo.',
					'type'		=>	'warning'
				);
			break;
			case 'admin_changepassw_success': 
				return array(
					'message'	=> 	'La contraseña de '.$opt.' ha sido cambiada exitosamente.',
					'type'		=>	'success'
				);
			break;
			case 'admin_changepassw_warning': 
				return array(
					'message'	=> 	'La contraseña no pudo ser cambiada. Intente de nuevo.',
					'type'		=>	'warning'
				);
			break;
			//Common
			case 'register_edit_error':
			case 'register_add_error': 
			case 'register_delete_error': 
			case 'admin_changepassw_error':
				return array(
					'message'	=> 	'Ocurrió un error inesperado en la base de datos. Intente de nuevo.',
					'type'		=>	'error'
				);
			break;
			case 'not_allowed':
				return array(
					'message'	=> 	'Operación no permitida.',
					'type'		=>	'error'
				);
			break;
		}
	}

	public static function show($msg = null, $type = 'info')
	{
		$notification = self::choose(Session::get('notification'), Session::get('opt'));

		if(!empty($msg))
			$notification = array(
				'message'	=> $msg,
				'type'		=> $type
			);
		elseif(empty($notification)) return;

		$msg 	= $notification['message'];
		$type	= $notification['type'];

		echo 	'<div class="alert-messages"><div class="message '.$type.'">'.
				'<button type="button" class="close" data-dismiss="alert">×</button>'.
				'<span>'.$msg.'</span></div></div>';

	}

	public static function show_errors_inline()
	{
		if(!Session::has('errors')) return;

		$start 	= '<div class="row"><div class="span5" style="margin-top:80px;">'.
				  '<div class="alert alert-block alert-error fade in">'.
				  '<button type="button" class="close" data-dismiss="alert">×</button>'.
                  '<h4 class="alert-heading">La operación no pudo completarse.</h4><p>';
        $end	= '</br>Vuelva a intentarlo por favor.</p></div></div></div>';

        foreach (Session::get('errors')->all() as $error)
        	$start .= '* ' . $error . '</br>';

		echo $start . $end;
	}
}